//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.cn.common.utils.excel;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.cn.common.utils.date.DateUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExcelUtil extends EasyExcelFactory {
    public ExcelUtil() {
    }

    public static void importExcel(InputStream inputStream, Class head, ReadListener readListener) {
        read(inputStream, head, readListener).sheet().doRead();
    }

    public static void exportExcel(HttpServletResponse response, Class clazz, String fileName, List<?> list) {
        baseExportExcel(response, clazz, fileName, list, new ArrayList());
    }

    public static void exportExcel(HttpServletResponse response, Class clazz, String fileName, List<?> list, List<String> excludeColumns) {
        baseExportExcel(response, clazz, fileName, list, excludeColumns);
    }

    public static void baseExportExcel(HttpServletResponse response, Class clazz, String fileName, List<?> list, List<String> excludeColumns) {
        try {
            String filename = URLEncoder.encode(DateUtil.getDate(new Date(), "yyyyMMddHHmmss") + fileName + ".xlsx", "UTF-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            ExcelWriter excelWriter = ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)write(response.getOutputStream()).registerWriteHandler(new ExcelColumnWidthHandler())).registerWriteHandler(new ExcelSheetWriteHandler())).registerWriteHandler(new ExcelStyleHandler())).excludeColumnFieldNames(excludeColumns)).build();
            WriteSheet writeSheet = ((ExcelWriterSheetBuilder)writerSheet(0, fileName).head(clazz)).build();
            excelWriter.write(list, writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static ExcelWriter exportExcels(HttpServletResponse response, String fileName) {
        try {
            String filename = URLEncoder.encode(DateUtil.getDate(new Date(), "yyyyMMddHHmmss") + fileName + ".xlsx", "UTF-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            return ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)write(response.getOutputStream()).registerWriteHandler(new ExcelColumnWidthHandler())).registerWriteHandler(new ExcelSheetWriteHandler())).registerWriteHandler(new ExcelStyleHandler())).build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static WriteSheet createSheet(Integer sheetNo, String sheetName, Class clazz) {
        return ((ExcelWriterSheetBuilder)writerSheet(sheetNo, sheetName).head(clazz)).build();
    }

    public static FileInputStream exportExcelToOSS(Class clazz, String fileName, List<?> list) {
        try {
            String tmp = System.getProperty("java.io.tmpdir");
            File file = new File(tmp + fileName + ".xlsx");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ExcelWriter excelWriter = ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)write(fileOutputStream).registerWriteHandler(new ExcelColumnWidthHandler())).registerWriteHandler(new ExcelSheetWriteHandler())).registerWriteHandler(new ExcelStyleHandler())).build();
            WriteSheet writeSheet = ((ExcelWriterSheetBuilder)writerSheet(0, fileName).head(clazz)).build();
            excelWriter.write(list, writeSheet);
            excelWriter.finish();
            return new FileInputStream(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
